package com.xxx.car.web.admin;

import com.github.pagehelper.PageInfo;
import com.xxx.car.entity.vo.request.ReqUserAdminQuery;
import com.xxx.car.entity.vo.request.ReqUserAdminUpdate;
import com.xxx.car.entity.vo.request.ReqUserCarAdminQuery;
import com.xxx.car.entity.vo.request.ReqUserWalletAdminQuery;
import com.xxx.car.entity.vo.response.RespUserWallet;
import com.xxx.car.service.CouponService;
import com.xxx.car.service.UserCarService;
import com.xxx.car.service.UserService;
import com.xxx.car.service.UserWalletService;
import com.xxx.common.base.vo.APIResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by yxj on 17/6/26.
 */
@Controller
@RequestMapping("/admin/user")
public class AdminUserController {

	@Autowired
	UserService userService;
	@Autowired
	UserCarService userCarService;
	@Autowired
	UserWalletService userWalletService;
	@Autowired
	CouponService couponService;

	@RequestMapping(value = "/")
	public String appUserList(Model model, @ModelAttribute ReqUserAdminQuery query) {
		PageInfo pageInfo = userService.queryAppUserListByAdmin(query);
		Integer normalCount = userService.queryAppNormalUserCount();
		Integer frozenCount = userService.queryAppFrozenUserCount();
		//获得当前页
		model.addAttribute("pageNum", pageInfo.getPageNum());
		//获得一页显示的条数
		model.addAttribute("pageSize", pageInfo.getPageSize());
		//是否是第一页
		model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
		//获得总页数
		model.addAttribute("totalPages", pageInfo.getPages());
		//是否是最后一页
		model.addAttribute("isLastPage", pageInfo.isIsLastPage());
		//前台对象
		model.addAttribute("userList", pageInfo.getList());
		model.addAttribute("normalCount", normalCount);
		model.addAttribute("frozenCount", frozenCount);
		//查询条件
		model.addAttribute("query",query);
		return "appUserList";
	}

	@RequestMapping(value = "/carList")
	public String carList(Model model, @ModelAttribute ReqUserCarAdminQuery query) {
		PageInfo pageInfo = userCarService.queryUserCarListForAdmin(query);
		//获得当前页
		model.addAttribute("pageNum", pageInfo.getPageNum());
		//获得一页显示的条数
		model.addAttribute("pageSize", pageInfo.getPageSize());
		//是否是第一页
		model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
		//获得总页数
		model.addAttribute("totalPages", pageInfo.getPages());
		//是否是最后一页
		model.addAttribute("isLastPage", pageInfo.isIsLastPage());
		//前台对象
		model.addAttribute("userCarList", pageInfo.getList());
		//查询条件
		model.addAttribute("query",query);
		return "userCarList";
	}

	@RequestMapping(value = "/wallet")
	public String userWallet(Model model, @ModelAttribute ReqUserWalletAdminQuery query) {
		RespUserWallet respUserWallet = userWalletService.getByUser(query.getUserId());
		Integer respUserCouponCount = couponService.queryUserCouponCount(query.getUserId());
		//前台对象
		model.addAttribute("respUserWallet", respUserWallet);
		model.addAttribute("respUserCouponCount", respUserCouponCount);
		return "userWallet";
	}

	@RequestMapping(value = "/userDetail")
	public String userDetail(Model model, @ModelAttribute ReqUserAdminUpdate update) {
		APIResponse response = userService.userDetail(update.getUserId());
		if (response.isRet()){
			//前台对象
			model.addAttribute("userDetail", response.getData());
		}
		return "userUpdate";
	}

	@RequestMapping(value = "/modifyUser")
	public String modifyUser(Model model, @ModelAttribute ReqUserAdminUpdate update) {
		userService.modifyUser(update.getUserId(),update.getLoginName(),update.getPassword(),update.getIsDelete(),update.getGroupId());
		return "redirect:/admin/user/";
	}

	@RequestMapping(value = "/modifyAdminUser")
	public String modifyAdminUser(Model model, @ModelAttribute ReqUserAdminUpdate update) {
		userService.modifyUser(update.getUserId(),update.getLoginName(),update.getPassword(),update.getIsDelete(),update.getGroupId());
		return "redirect:/admin/user/adminUserList";
	}

	@RequestMapping(value = "/adminUserList")
	public String adminUserList(Model model, @ModelAttribute ReqUserAdminQuery query) {
		PageInfo pageInfo = userService.queryAdminUserListByAdmin(query);

		//获得当前页
		model.addAttribute("pageNum", pageInfo.getPageNum());
		//获得一页显示的条数
		model.addAttribute("pageSize", pageInfo.getPageSize());
		//是否是第一页
		model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
		//获得总页数
		model.addAttribute("totalPages", pageInfo.getPages());
		//是否是最后一页
		model.addAttribute("isLastPage", pageInfo.isIsLastPage());
		//前台对象
		model.addAttribute("userList", pageInfo.getList());

		return "adminUserList";
	}

	@RequestMapping(value = "/adminUserAdd")
	public String adminUserAdd(Model model, @ModelAttribute ReqUserAdminUpdate update) {
		if (update == null){
			return "adminUserAdd";
		}
		if (update.getLoginName() == null || "".equals(update.getLoginName())){
			return "adminUserAdd";
		}
		if (update.getPassword() == null || "".equals(update.getPassword())){
			return "adminUserAdd";
		}
		if (update.getGroupId() == null || "".equals(update.getGroupId())){
			return "adminUserAdd";
		}
		try{
			userService.addAdminUser(update.getLoginName(),update.getPassword(),update.getGroupId());
			return "redirect:/admin/user/adminUserList";
		}catch (Exception e){
			return "adminUserAdd";
		}
	}
}
